const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {
    CleanWebpackPlugin
} = require('clean-webpack-plugin');
let htmlPlugins = [
    'index',
    'other'
].map(chunkName=>{
    return new HtmlWebpackPlugin({
        template:`./src/${chunkName}.html`,
        filename: `${chunkName}.html`,
        chunks:[chunkName],
    })
});
module.exports = {
    mode: 'development',
    entry: {
        index:'./src/js/index.js',
        other:'./src/js/other.js',
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename:'js/[name].[contentHash:8].js'
    },
    module: {
        rules: [{
                test: /\.scss/,
                include: path.resolve(__dirname, 'src'),
                exclude: /node_modules/,
                use: [{
                    loader: MiniCssExtractPlugin.loader,
                }, 'css-loader', 'sass-loader']
            }, {
                test: /\.html/,
                use: 'html-withimg-loader'
            },
            
            {
                test: /\.(jpg|png|gif)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 2,
                        outputPath: 'img'
                    }
                }
            },
        ]
    },
    plugins: [
        ...htmlPlugins,
        new MiniCssExtractPlugin({
            filename: 'css/[name].css',
            chunkFilename: 'css/[id].css'
        }),
        new CleanWebpackPlugin()
    ],
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        host: '127.0.0.1',
        compress: true,
        port: 8081
    }
}